/**
 * 
 * 分页器
 * 2019.05.27
 * 
 */
;(function($) {
	
	$.fn.pagination=function(options){
    	//创建对象
		var text = new textModal(this, options);
		text.html();
	}
	
	//构造函数
    var textModal = function(ele, opt) {   	
    	//选择器对象    	
        this.$element=ele;   
        //默认参数
        this.defaults = {
    		totalPage:1,//总页数
    		callback:function(){},//回调函数
    		current:1,//当前页数，默认1
    		showResult:0,//默认不展示查询结果
    		resultCalculate:0,//查询数据数量
        },        
        //初始化参数
        this.options = $.extend({}, this.defaults, opt);
    }
	
    //方法
    textModal.prototype = {
    	//添加模态代码
    	html:function(){
    		var $t=this;
    		var $opt=$t.options;
    		
    		var html=''
    		var pagination='<div class="doubleOper btn btn-white" id="last">'+$.i18n.propJsp("上一页")+'</div>'+
							'<div class="pages btn btn-white" id="1">1</div>'+
							'<div class="doubleOper btn btn-white" id="next">'+$.i18n.propJsp("下一页")+'</div>'+
							'<div class="totalPages btn btn-white disabled">'+$.i18n.propJsp("共")+'<em id="total"></em>'+$.i18n.propJsp("页")+'</div>'
    		if($opt.showResult==0){
    			html=pagination
    		}else{
    			html='<div class="pagination_bottom flex">'+
    					'<div class="pagination_result inlineflex">'+$.i18n.propJsp("共查询到")+'<div class="pagination_total_quantity">'+$opt.resultCalculate+'</div>'+$.i18n.propJsp("条数据")+'</div>'+
    					pagination+
    				'</div>'
    		}
    		
			$t.$element.addClass("pagination")
    		$t.$element.empty().html(html);
    		$t.appendHtml();
		},
		appendHtml:function(){
    		var $t=this;
    		var $opt=$t.options;
    		var el=$t.$element;
    		
    		if($opt.totalPage>1){
    			for(var i=2;i<=$opt.totalPage;i++){
    				el.find("#"+(i-1)).after('<div class="pages btn btn-white" id="'+i+'">'+i+'</div>')
    			}
    			//如果页数过多则隐藏中间页码
    			if($opt.totalPage>10){
					if($opt.current>4&&$opt.current<($opt.totalPage-3)){
						$(".pages").css("display","none").addClass("neglect")
						$(".pages:lt(2),.pages:gt("+($opt.totalPage-3)+")").css("display","block").removeClass("neglect")
						$(".pages:eq(1)").after('<div class="etc">......</div>')
						$(".pages:eq("+($opt.current-1)+")").css("display","block").removeClass("neglect").after('<div class="etc">......</div>')
					}else{
						$(".pages").css("display","none").addClass("neglect")
	    				$(".pages:lt(4),.pages:gt("+($opt.totalPage-5)+")").css("display","block").removeClass("neglect")
	    				$(".pages:eq(4)").after('<div class="etc">......</div>')
					}
    			}
    		}   
    		
    		var totalpage=$opt.totalPage==0?1:$opt.totalPage;
    		$(".pagination em").html(totalpage)
    		
    		$("#pagination #"+$opt.current).addClass("btn-primary")
    		
    		$("head").append('<style>'+
    							'.pagination{width:100%;display:flex;align-items:center;justify-content:center;padding:10px 0;}'+
    							'.pagination *{margin:0 2px;}'+
    							'.pagination_bottom{width:calc( 100% - 8px );padding-left: 4px;}'+
    							'.pagination_result{margin-right:auto;color:#999;}'+
    						'</style>')
    		
    		$t.bindEventListener()
    		
    		$t.bindEvent()
    	},
    	//绑定事件
    	bindEvent:function(){
    		var $t=this;
    		var $opt=$t.options;
    		var el=$t.$element;
    		
    		//上一页
    		el.find("#last").unbind("click").click(function(){
    			if($opt.current>1){
    				$opt.current=Number($opt.current)-1
    				$(".pages.btn-primary").removeClass("btn-primary")
    				$("#pagination #"+$opt.current).addClass("btn-primary")
    				$opt.callback($opt.current)
    				$t.bindEventListener()
    			}
    		})
    		//下一页
    		el.find("#next").unbind("click").click(function(){
    			if($opt.current<$opt.totalPage){
    				$opt.current=Number($opt.current)+1
    				$(".pages.btn-primary").removeClass("btn-primary")
    				$("#pagination #"+$opt.current).addClass("btn-primary")
    				$opt.callback($opt.current)
    				$t.bindEventListener()
    			}
    		})
    		//翻页
    		el.find(".pages").unbind("click").click(function(){
    			var current=$(this).attr("id")
    			if($opt.current!=current){
    				$opt.current=current
    				$(".pages.btn-primary").removeClass("btn-primary")
    				$("#pagination #"+$opt.current).addClass("btn-primary")
    				$opt.callback($opt.current)
    				$t.bindEventListener()
    			}
    		})
    		
    	},
    	bindEventListener:function(){
    		var $t=this;
    		var $opt=$t.options;
    		var el=$t.$element;
    		
    		//设置不可点
    		if($opt.current==1){
    			el.find("#last").attr("disabled","disabled").addClass("disabled")
    		}else{
				el.find("#last").removeAttr("disabled").removeClass("disabled")
    		}
    		if($opt.current==$opt.totalPage){
    			el.find("#next").attr("disabled","disabled").addClass("disabled")
    		}else{
    			el.find("#next").removeAttr("disabled").removeClass("disabled")
    		}
    		
    		//如果只有一页数据
    		if($opt.totalPage==0||$opt.totalPage==1){
				el.find("#next").attr("disabled","disabled").addClass("disabled")
			}else{
				el.find("#next").removeAttr("disabled").removeClass("disabled")
			}
    		
    		//如果最后一页数据
    		if($opt.totalPage==$opt.current||$opt.totalPage==0){
				el.find("#next").attr("disabled","disabled").addClass("disabled")
			}else{
				el.find("#next").removeAttr("disabled").removeClass("disabled")
			}
    	},
    	//获取项目路径
		getContextPath:function(){
			var pathName = document.location.pathname;
			var index = pathName.substr(1).indexOf("/");
			var path = pathName.substr(0, index + 1);
			return path;
		},
    	
    }
	
})(jQuery);